package com.chuangping.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chuangping.R;
import com.chuangping.di.component.DaggerLoginComponent;
import com.chuangping.di.module.LoginModule;
import com.chuangping.mvp.contract.LoginContract;
import com.chuangping.mvp.presenter.LoginPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.nameEdit)
    EditText mEtPhoneNum;
    @BindView(R.id.pwdEdit)
    EditText mPwdEdit;
    @BindView(R.id.loginButton)
    Button mLoginBtn;

    private boolean isInputPhone;
    private boolean isInputPassword;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLoginComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @OnClick({R.id.loginButton})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.loginButton:
              /*  mPresenter.login(mEtPhoneNum.getText().toString().trim()
                        , mPwdEdit.getText().toString().trim());*/
                startActivity(new Intent(this, HomeActivity.class));
                break;
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mEtPhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                isInputPhone = !TextUtils.isEmpty(s);
                switchLoginButton();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mPwdEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                isInputPassword = !TextUtils.isEmpty(s);
                switchLoginButton();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void switchLoginButton() {
        mLoginBtn.setEnabled(isInputPassword && isInputPhone);
    }


    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

}